from atexit import register
from selenium.webdriver.common.by import By
from common.Utils import ForumDriver

class ForumLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:9580/sign-in.html"
        self.driver=ForumDriver.driver
        self.driver.get(self.url)
        # 添加隐式等待
        self.driver.implicitly_wait(3)

    #注册失败测试
    def RegisterFailTest(self):
        #点击注册按钮进入注册页面
        self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").click()
        # 密码和确认密码不一致
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("Neymar")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("Neymar")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("1234")
        #点击同意协议，点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #屏幕截图
        ForumDriver.getScreeShot()

        # #用户名已存在
        # self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("KaKa")
        # self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("KaKa")
        # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        # # 点击同意协议，点击注册按钮
        # self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # # 屏幕截图
        # ForumDriver.getScreeShot()

    #注册成功测试
    def RegisterSucTest(self):#
        # 点击注册按钮进入注册页面
        self.driver.find_element(By.CSS_SELECTOR,"body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").click()
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Neymar")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("Neymar")
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("123")
        # 点击同意协议，点击注册按钮
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #预期结果注册成功 跳转回登录界面
        # 定位登录按钮 屏幕截图
        self.driver.find_element(By.CSS_SELECTOR, "#submit")
        ForumDriver.getScreeShot()

    #登录失败测试
    def LoginFailTest(self):
        #输入正确的用户名 错误的密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("Neymar")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 预期登录结果失败，仍在登录页面，且系统提示用户名或密码输入错误
        # 截图验证提示信息，定位登录按钮验证还在登录页面
        ForumDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "#submit")

    # 登录成功测试
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("Neymar")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 预期登录成功，进入首页
        # 截图验证提示信息，定位首页按钮验证登录成功，进入首页
        ForumDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a")

#创建类对象
# login = ForumLogin()
# #注册失败测试
# login.RegisterFailTest()
# #注册成功测试
# login.RegisterSucTest()
# #登录失败测试
# login.LoginFailTest()
# #登录成功测试
# login.LoginSucTest()